<template>
    <el-dialog
    :title="title"
    :visible="visible"
    @update:visible="$emit('update:visible', $event)"
    @closed="$emit('clear')"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="name" label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        prop="description"
        label="角色描述"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, editRoleById } from '@/api/setting'
export default {
    name:'SetDiglog',
    props:{
        visible:Boolean,
        form:Object
    },
    computed:{
      title(){
        return this.form.id?'编辑角色':'新增角色'
      }
    },
    data() {
      return {
        formLabelWidth:'120px',
        rules:{
          name:[{ required: true, message: '不能为空', trigger: 'blur' }],
           description: [{ required: true, message: '不能为空', trigger: 'blur' }]
        }
      }
    },
    methods:{
      submitForm(){
        this.$refs.form.validate(async valid=>{
          if(valid){
            if(this.form.id){
              const res=await editRoleById(this.form)
            }else{
              await addRole(this.form)
            }
            this.$emit('getData')
            this.$emit('update:visible',false)
            this.$message.success(`${this.title} ok`)
          }else{
            this.$message.error('格式错误')
            return false
          }
        })
      }
    }
}
</script>

<style>

</style>